
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "font.h"
#include "font_typedef.h"
#include "../memory.h"


//取模方式：阴码、逆向、列行式


static font_t font_glb[font_type_max]={0};

void font_init(void)
{
  font_glb[font_type_en] = font_8x16;
  font_glb[font_type_ch] = font_16x16;
}

uint8_t font_get_char_num(const char* str)
{
  uint8_t cnt = 0;
  while (*str) {
      if ((*str & 0xE0) == 0xE0) {
          // 这是一个中文字符，占用3个字节
          printf("中文字符: %x%x%x\n", str[0], str[1], str[2]);
          str += 3;
          cnt++;
      } else {
          // 这是一个英文字符，占用1个字节
          printf("英文字符: %c\n", *str);
          str++;
          cnt++;
      }
    }
  return cnt;
}

#define FOREACH_STRING(base_str, word_str, is_cn, body)\
do{\
  word_str = (char*)base_str;\
  printf("base_str:%s\r\n", base_str);\
  uint8_t inc = 0;\
  while (*word_str) {\
      printf("word_str:%x\r\n", word_str[0]);\
      if ((word_str[0] & 0xE0) == 0xE0) {\
          printf("chinese: %x%x%x\n", (word_str)[0], (word_str)[1], (word_str)[2]);\
          inc = 3;\
          (is_cn) = 1;\
      } else {\
          printf("ascii: %c\n", *word_str);\
          inc = 1;\
          (is_cn) = 0;\
      }\
      (body);\
      word_str+=inc;\
    }\
}while(0);

font_data_desc_t font_get_cn_fontdata(font_t* cn_font, const char* str)
{
  font_data_desc_t desc = {0};
  uint8_t ch_size = strlen("一");
  int i;

  printf("str:%s\r\n", str);
  for(int i=0;i<3;i++)
  {
    printf("%x\r\n", str[i]);
  }
  printf("\r\n");

  printf("loop_str:\r\n");
  for(int i=0;i<cn_font->font_lookup_len;i++)
  {
    printf("%x ", cn_font->font_lookup_str[i]);
  }
  printf("\r\n");

  printf("loop_len:%d\r\n", cn_font->font_lookup_len);
  for(i = 0; i < cn_font->font_lookup_len; i+=ch_size)
  {
    if (((uint8_t)str[0] == cn_font->font_lookup_str[i])&&
        ((uint8_t)str[1] == cn_font->font_lookup_str[i+1])&&
        ((uint8_t)str[2] == cn_font->font_lookup_str[i+2]))
    {
      i=i/ch_size;
      desc.font_data = (uint8_t*)&cn_font->font_data[i*cn_font->font_height/8*cn_font->font_width];
      desc.font_data_len = cn_font->font_height/8*cn_font->font_width;
      desc.font_height = cn_font->font_height;
      desc.font_width = cn_font->font_width;
      printf("ch index:%d\r\n", i);
      break;
    }
  }
  if(i==cn_font->font_lookup_len)
  {
    printf("not find font\r\n");
  }
  return desc;
}

font_data_desc_t font_get_en_fontdata(font_t* en_font, const char *str)
{
  font_data_desc_t desc = {0};

  uint8_t index = (str[0]) - ' ';
  printf("en index:%d\r\n", index);
  desc.font_data = (uint8_t*)&en_font->font_data[index*en_font->font_height/8*en_font->font_width];
  desc.font_data_len = en_font->font_height/8*en_font->font_width;
  desc.font_height = en_font->font_height;
  desc.font_width = en_font->font_width;
  return desc;
}

font_data_str_data_t font_get_fontdata(const char* str)
{
  font_data_str_data_t fontStrData;
  uint8_t word_num = 0, is_cn = 0;
  char* word_ptr;

  FOREACH_STRING(str, word_ptr, is_cn, ({
    word_num++;
  }));

  printf("words num:%d\r\n", word_num);

  fontStrData.font_desc = memory_alloc(word_num * sizeof(font_data_desc_t));
  fontStrData.font_cnt = 0;

  FOREACH_STRING(str, word_ptr, is_cn,({
    if(is_cn){
      fontStrData.font_desc[fontStrData.font_cnt] = font_get_cn_fontdata(&font_glb[font_type_ch], word_ptr);
    }
    else{
      fontStrData.font_desc[fontStrData.font_cnt] = font_get_en_fontdata(&font_glb[font_type_en], word_ptr);
    }
    fontStrData.font_cnt++;
  }));

  return fontStrData;
}

void font_free_fontdata(font_data_str_data_t font_data)
{
  memory_free(font_data.font_desc);
  font_data.font_cnt = 0;
}

//#define TEST
#ifdef TEST
int main(void)
{
  font_init();

  font_data_str_data_t strData = font_get_fontdata("123公众号456");
  for(int i=0;i<strData.font_cnt;i++)
  {
    printf("font_data_len %d\r\n",strData.font_desc[i].font_data_len);
    printf("font_height %d\r\n",strData.font_desc[i].font_height);
    printf("font_width %d\r\n",strData.font_desc[i].font_width);
    for(int j=0;j<strData.font_desc[i].font_data_len;j++)
    {
      printf("0x%x ",strData.font_desc[i].font_data[j]);
      if(i>1 && (i%16==0))
      {
        printf("\r\n");
      }
    }
    printf("\r\n");
  }
  font_free_fontdata(strData);

}

#endif